﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Sudoku.Resources;

namespace Sudoku
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            level1.IsChecked = true;
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }
        int[,] number ={
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
        };
        bool[,] text = new bool[9, 9];
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            int inter = 1000;
            TextBox[,] textbox ={
                {this.a1,this.a2,this.a3,this.a4,this.a5,this.a6,this.a7,this.a8,this.a9},
                {this.b1,this.b2,this.b3,this.b4,this.b5,this.b6,this.b7,this.b8,this.b9},
                {this.c1,this.c2,this.c3,this.c4,this.c5,this.c6,this.c7,this.c8,this.c9},
                {this.d1,this.d2,this.d3,this.d4,this.d5,this.d6,this.d7,this.d8,this.d9},
                {this.e1,this.e2,this.e3,this.e4,this.e5,this.e6,this.e7,this.e8,this.e9},
                {this.f1,this.f2,this.f3,this.f4,this.f5,this.f6,this.f7,this.f8,this.f9},
                {this.g1,this.g2,this.g3,this.g4,this.g5,this.g6,this.g7,this.g8,this.g9},
                {this.h1,this.h2,this.h3,this.h4,this.h5,this.h6,this.h7,this.h8,this.h9},
                {this.i1,this.i2,this.i3,this.i4,this.i5,this.i6,this.i7,this.i8,this.i9},
            };
            for (int i = 0; i < textbox.GetLength(0); i++)
            {
                for (int j = 0; j < textbox.GetLength(1); j++)
                {
                    if (textbox[i, j].Text != "")
                    {
                        number[i, j] = Convert.ToInt32(textbox[i, j].Text);
                    }
                }
            }
            for (int p = 0; p < inter; p++)
            {
                int flag = 0;
                Random rd = new Random();
                int num = rd.Next(1, 10);
                int row = rd.Next(0, 9);
                int colu = rd.Next(0, 9);
                //检测行
                for (int j = 0; j < 9; j++)
                    if (num == number[row, j]) flag = 1;
                //检测列
                for (int i = 0; i < 9; i++)
                    if (num == number[i, colu]) flag = 1;
                //检测举行区域
                //获得左上角的坐标
                int l = row / 3 * 3;
                int k = colu / 3 * 3;
                //循环比较
                for (int i = l; i < l + 3; i++)
                    for (int j = k; j < k + 3; j++)
                        if (num == number[row, colu]) flag = 1;
                if (flag == 0 && text[row, colu] == false) { number[row, colu] = num; text[row, colu] = true; }
                else continue;
            }
            a1.Text = number[0, 0].ToString(); a2.Text = number[0, 1].ToString(); a3.Text = number[0, 2].ToString();
            a4.Text = number[0, 3].ToString(); a5.Text = number[0, 4].ToString(); a6.Text = number[0, 5].ToString();
            a7.Text = number[0, 6].ToString(); a8.Text = number[0, 7].ToString(); a9.Text = number[0, 8].ToString();
            b1.Text = number[1, 0].ToString(); b2.Text = number[1, 1].ToString(); b3.Text = number[1, 2].ToString();
            b4.Text = number[1, 3].ToString(); b5.Text = number[1, 4].ToString(); b6.Text = number[1, 5].ToString();
            b7.Text = number[1, 6].ToString(); b8.Text = number[1, 7].ToString(); b9.Text = number[1, 8].ToString();
            c1.Text = number[2, 0].ToString(); c2.Text = number[2, 1].ToString(); c3.Text = number[2, 2].ToString();
            c4.Text = number[2, 3].ToString(); c5.Text = number[2, 4].ToString(); c6.Text = number[2, 5].ToString();
            c7.Text = number[2, 6].ToString(); c8.Text = number[2, 7].ToString(); c9.Text = number[2, 8].ToString();
            d1.Text = number[3, 0].ToString(); d2.Text = number[3, 1].ToString(); d3.Text = number[3, 2].ToString();
            d4.Text = number[3, 3].ToString(); d5.Text = number[3, 4].ToString(); d6.Text = number[3, 5].ToString();
            d7.Text = number[3, 6].ToString(); d8.Text = number[3, 7].ToString(); d9.Text = number[3, 8].ToString();
            e1.Text = number[4, 0].ToString(); e2.Text = number[4, 1].ToString(); e3.Text = number[4, 2].ToString();
            e4.Text = number[4, 3].ToString(); e5.Text = number[4, 4].ToString(); e6.Text = number[4, 5].ToString();
            e7.Text = number[4, 6].ToString(); e8.Text = number[4, 7].ToString(); e9.Text = number[4, 8].ToString();
            f1.Text = number[5, 0].ToString(); f2.Text = number[5, 1].ToString(); f3.Text = number[5, 2].ToString();
            f4.Text = number[5, 3].ToString(); f5.Text = number[5, 4].ToString(); f6.Text = number[5, 5].ToString();
            f7.Text = number[5, 6].ToString(); f8.Text = number[5, 7].ToString(); f9.Text = number[5, 8].ToString();
            g1.Text = number[6, 0].ToString(); g2.Text = number[6, 1].ToString(); g3.Text = number[6, 2].ToString();
            g4.Text = number[6, 3].ToString(); g5.Text = number[6, 4].ToString(); g6.Text = number[6, 5].ToString();
            g7.Text = number[6, 6].ToString(); g8.Text = number[6, 7].ToString(); g9.Text = number[6, 8].ToString();
            h1.Text = number[7, 0].ToString(); h2.Text = number[7, 1].ToString(); h3.Text = number[7, 2].ToString();
            h4.Text = number[7, 3].ToString(); h5.Text = number[7, 4].ToString(); h6.Text = number[7, 5].ToString();
            h7.Text = number[7, 6].ToString(); h8.Text = number[7, 7].ToString(); h9.Text = number[7, 8].ToString();
            i1.Text = number[8, 0].ToString(); i2.Text = number[8, 1].ToString(); i3.Text = number[8, 2].ToString();
            i4.Text = number[8, 3].ToString(); i5.Text = number[8, 4].ToString(); i6.Text = number[8, 5].ToString();
            i7.Text = number[8, 6].ToString(); i8.Text = number[8, 7].ToString(); i9.Text = number[8, 8].ToString();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            int inter = 1000;
            TextBox[,] textbox ={
                {this.a1,this.a2,this.a3,this.a4,this.a5,this.a6,this.a7,this.a8,this.a9},
                {this.b1,this.b2,this.b3,this.b4,this.b5,this.b6,this.b7,this.b8,this.b9},
                {this.c1,this.c2,this.c3,this.c4,this.c5,this.c6,this.c7,this.c8,this.c9},
                {this.d1,this.d2,this.d3,this.d4,this.d5,this.d6,this.d7,this.d8,this.d9},
                {this.e1,this.e2,this.e3,this.e4,this.e5,this.e6,this.e7,this.e8,this.e9},
                {this.f1,this.f2,this.f3,this.f4,this.f5,this.f6,this.f7,this.f8,this.f9},
                {this.g1,this.g2,this.g3,this.g4,this.g5,this.g6,this.g7,this.g8,this.g9},
                {this.h1,this.h2,this.h3,this.h4,this.h5,this.h6,this.h7,this.h8,this.h9},
                {this.i1,this.i2,this.i3,this.i4,this.i5,this.i6,this.i7,this.i8,this.i9},
            };
            for (int i = 0; i < textbox.GetLength(0); i++)
            {
                for (int j = 0; j < textbox.GetLength(1); j++)
                {
                    if (textbox[i, j].Text != "")
                    {
                        number[i, j] = Convert.ToInt32(textbox[i, j].Text);
                    }
                }
            }
            for (int p = 0; p < inter; p++)
            {
                int flag = 0;
                Random rd = new Random();
                int num = rd.Next(1, 10);
                int row = rd.Next(0, 9);
                int colu = rd.Next(0, 9);
                //检测行
                for (int j = 0; j < 9; j++)
                    if (num == number[row, j]) flag = 1;
                //检测列
                for (int i = 0; i < 9; i++)
                    if (num == number[i, colu]) flag = 1;
                //检测举行区域
                //获得左上角的坐标
                int l = row / 3 * 3;
                int k = colu / 3 * 3;
                //循环比较
                for (int i = l; i < l + 3; i++)
                    for (int j = k; j < k + 3; j++)
                        if (num == number[row, colu]) flag = 1;
                if (flag == 0 && text[row, colu] == false) { number[row, colu] = num; text[row, colu] = true; }
                else continue;
            }
            a1.Text = number[0, 0].ToString(); a2.Text = number[0, 1].ToString(); a3.Text = number[0, 2].ToString();
            a4.Text = number[0, 3].ToString(); a5.Text = number[0, 4].ToString(); a6.Text = number[0, 5].ToString();
            a7.Text = number[0, 6].ToString(); a8.Text = number[0, 7].ToString(); a9.Text = number[0, 8].ToString();
            b1.Text = number[1, 0].ToString(); b2.Text = number[1, 1].ToString(); b3.Text = number[1, 2].ToString();
            b4.Text = number[1, 3].ToString(); b5.Text = number[1, 4].ToString(); b6.Text = number[1, 5].ToString();
            b7.Text = number[1, 6].ToString(); b8.Text = number[1, 7].ToString(); b9.Text = number[1, 8].ToString();
            c1.Text = number[2, 0].ToString(); c2.Text = number[2, 1].ToString(); c3.Text = number[2, 2].ToString();
            c4.Text = number[2, 3].ToString(); c5.Text = number[2, 4].ToString(); c6.Text = number[2, 5].ToString();
            c7.Text = number[2, 6].ToString(); c8.Text = number[2, 7].ToString(); c9.Text = number[2, 8].ToString();
            d1.Text = number[3, 0].ToString(); d2.Text = number[3, 1].ToString(); d3.Text = number[3, 2].ToString();
            d4.Text = number[3, 3].ToString(); d5.Text = number[3, 4].ToString(); d6.Text = number[3, 5].ToString();
            d7.Text = number[3, 6].ToString(); d8.Text = number[3, 7].ToString(); d9.Text = number[3, 8].ToString();
            e1.Text = number[4, 0].ToString(); e2.Text = number[4, 1].ToString(); e3.Text = number[4, 2].ToString();
            e4.Text = number[4, 3].ToString(); e5.Text = number[4, 4].ToString(); e6.Text = number[4, 5].ToString();
            e7.Text = number[4, 6].ToString(); e8.Text = number[4, 7].ToString(); e9.Text = number[4, 8].ToString();
            f1.Text = number[5, 0].ToString(); f2.Text = number[5, 1].ToString(); f3.Text = number[5, 2].ToString();
            f4.Text = number[5, 3].ToString(); f5.Text = number[5, 4].ToString(); f6.Text = number[5, 5].ToString();
            f7.Text = number[5, 6].ToString(); f8.Text = number[5, 7].ToString(); f9.Text = number[5, 8].ToString();
            g1.Text = number[6, 0].ToString(); g2.Text = number[6, 1].ToString(); g3.Text = number[6, 2].ToString();
            g4.Text = number[6, 3].ToString(); g5.Text = number[6, 4].ToString(); g6.Text = number[6, 5].ToString();
            g7.Text = number[6, 6].ToString(); g8.Text = number[6, 7].ToString(); g9.Text = number[6, 8].ToString();
            h1.Text = number[7, 0].ToString(); h2.Text = number[7, 1].ToString(); h3.Text = number[7, 2].ToString();
            h4.Text = number[7, 3].ToString(); h5.Text = number[7, 4].ToString(); h6.Text = number[7, 5].ToString();
            h7.Text = number[7, 6].ToString(); h8.Text = number[7, 7].ToString(); h9.Text = number[7, 8].ToString();
            i1.Text = number[8, 0].ToString(); i2.Text = number[8, 1].ToString(); i3.Text = number[8, 2].ToString();
            i4.Text = number[8, 3].ToString(); i5.Text = number[8, 4].ToString(); i6.Text = number[8, 5].ToString();
            i7.Text = number[8, 6].ToString(); i8.Text = number[8, 7].ToString(); i9.Text = number[8, 8].ToString();
            MessageBox.Show("You have  been reminded Succseefully");

        }

        private void level1_Checked(object sender, RoutedEventArgs e)
        {
            level2.IsEnabled = false;
            level3.IsEnabled = false;
        }

        private void level2_Checked(object sender, RoutedEventArgs e)
        {
            level1.IsEnabled = false;
            level3.IsEnabled = false;
        }

        private void level3_Checked(object sender, RoutedEventArgs e)
        {
            level2.IsEnabled = false;
            level1.IsEnabled = false;
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            level3.IsEnabled = true;
            level1.IsEnabled = true;
            level2.IsEnabled = true;
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
        }
    }

    // Sample code for building a localized ApplicationBar
    //private void BuildLocalizedApplicationBar()
    //{
    //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
    //    ApplicationBar = new ApplicationBar();

    //    // Create a new button and set the text value to the localized string from AppResources.
    //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
    //    appBarButton.Text = AppResources.AppBarButtonText;
    //    ApplicationBar.Buttons.Add(appBarButton);

    //    // Create a new menu item with the localized string from AppResources.
    //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
    //    ApplicationBar.MenuItems.Add(appBarMenuItem);
    //}
}